package com.yksj.healthtalk.wallet;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.DoubleBtnFragmentDialog;
import com.yksj.healthtalk.comm.DoubleBtnFragmentDialog.OnDilaogClickListener;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog.OnClickSureBtnListener;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.setting.SettingPhoneBound;

/**
 * 余额
 * @author Administrator
 *
 */
public class MyBalanceFragment extends Fragment implements OnClickListener, OnDilaogClickListener,OnClickSureBtnListener{
	
	public static final int TOPUPCODE = 3;
	private Button settlementBtn;
	private TextView mBalanceTv;
	private boolean isSettlement = false;//能否结算
	private String settlementReminder;//不能结算的提示
	private String settlementMessage;//结算账户信息
	private Boolean isPayPwd;//是否设置支付密码
	private Boolean isBDPhone;//是否绑定手机
	private String isBDPhoneNum;//绑定手机号码
	private MyWalletMainActivity mMyWalletMainActivity;
	private Button mTopUp;//充值按钮
	private String money;
	private int doubleDialogFlag;//确定按钮点击时间的标识
	private boolean isNoticeSms;//是否开通了短信提醒
	private EditText mTopUpMoney;
	private int balance_info_code = -1;//结算信息：BALANCE_INFO_CODE   0不能结算/1能结算
	private View v;
	private String mBankNumber;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.my_balance_layout, null);
		initWidget(v); 
		initDate(v);
		return v;
	}
	
	@Override
	public void onAttach(Activity activity) {
		mMyWalletMainActivity = (MyWalletMainActivity) activity;
		super.onAttach(activity);
	}

	@Override
	public void onStart() {
		HttpRestClient.doHttpWalletBalance(null, new JsonsfHttpResponseHandler(getActivity()){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				//请求失败
				if (response.containsKey("INFO")) {
					SingleBtnFragmentDialog.showDefault(getFragmentManager(),  response.getString("INFO"));
					getActivity().onBackPressed();
					return;
				}
				v.setVisibility(View.VISIBLE);
				if (response.containsKey("YELLOW_BOY")) {
					money =  response.getString("YELLOW_BOY");
					mBalanceTv.setText("余额："+money+"元");
				}
				if (response.containsKey("BALANCE")) {
					if (response.getIntValue("BALANCE") == 1) {
						settlementBtn.setVisibility(View.VISIBLE);
					}else {
						settlementBtn.setVisibility(View.GONE);
					}
				}
//				结算信息：BALANCE_INFO  
//				   -----此次为首次申请结算，30天内只能结算一次   BALANCE_INFO_CODE=1时存在
//				   ------30天内只允许结算一次，您下次结算日期开始于yyyy年mm月dd日   BALANCE_INFO_CODE=0时存在
//				结算信息：BALANCE_INFO_CODE   0不能结算/1能结算
				if (response.containsKey("BALANCE_INFO")) {
//					if (response.getString("BALANCE_INFO").equals("")) {
//						isSettlement = true;
//					}else {
						settlementReminder = response.getString("BALANCE_INFO");
//					}
				}
				if (response.containsKey("BALANCE_INFO_CODE")) {
					 balance_info_code = response.getIntValue("BALANCE_INFO_CODE");
					switch (balance_info_code) {
					case 0:
						isSettlement = false;
						break;
					case 1:
						isSettlement = true;
						break;

					default:
						break;
					}
				}
				//短信提醒
				if (response.containsKey("BALANCE_NOTICE_SMS")) {
					if (response.getIntValue("BALANCE_NOTICE_SMS") == 1) {
						isNoticeSms = true;
					}else {
						isNoticeSms = false;
					}
				}
				
				if (response.containsKey("BALANCE_ACCOUNT_INFO")) {
					settlementMessage = response.getString("BALANCE_ACCOUNT_INFO");
				}
				
				//是否绑定手机
				if (response.containsKey("BD_TELEPHONE")) {
					if (response.getString("BD_TELEPHONE").equals("0")) {
						isBDPhone = false;
					}else {
						isBDPhone = true;
						isBDPhoneNum = response.getString("BD_TELEPHONE");
					}
				}
				//银行卡
				if (response.containsKey("BANK_ACCOUNT")) {
					mBankNumber=response.getString("BANK_ACCOUNT");
				}
				//是否设置支付密码
				if (response.containsKey("PAYMENT_PASS")) {
					if (response.getIntValue("PAYMENT_PASS") == 1) {
						isPayPwd = true;
					}else {
						isPayPwd = false;
					}
				}
				mMyWalletMainActivity.setInitDate(isPayPwd, isBDPhone, isBDPhoneNum,isNoticeSms);
				super.onSuccess(statusCode, response);
			}
		});
		super.onStart();
	}
	private void initDate(final View v) {
		v.setVisibility(View.GONE);
	}

	private void initWidget(View v) {
		settlementBtn = (Button) v.findViewById(R.id.settlement);
		mBalanceTv = (TextView)v.findViewById(R.id.balance);
		mTopUp = (Button)v.findViewById(R.id.top_up);
		mTopUpMoney = (EditText)v.findViewById(R.id.top_up_money);
		settlementBtn.setOnClickListener(this);
		mTopUp.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.settlement:
			if (money != null && money.equals("0")) {
				SingleBtnFragmentDialog.showDefault(getChildFragmentManager(),  "您的钱包没有余额，无需结算");
				return;
			}
			if (isBDPhone != null && isBDPhone == true) {//是否绑定手机
				if (isPayPwd == null || isPayPwd == false) {//是否设置支付密码
					doubleDialogFlag=1;
					DoubleBtnFragmentDialog.showDefault(getChildFragmentManager(), "使用钱包结算功能，需设置钱包支付密码，目前您尚未设置.", "稍后再说", "现在设置", this);
				}else {
						SingleBtnFragmentDialog.showDefault(getFragmentManager(),  settlementReminder,this);
				}
			}else {
				doubleDialogFlag=2;
				DoubleBtnFragmentDialog.showDefault(getFragmentManager(), "使用钱包结算功能，需绑定手机，目前您尚未绑定", "稍后再说", "现在绑定", this);
			}
			break;
		case R.id.top_up:
			String count = mTopUpMoney.getText().toString().trim();
			if (TextUtils.isEmpty(count) || count.equals("0")) {
				SingleBtnFragmentDialog.showDefault(getChildFragmentManager(), "请输入充值金额");
				return;
			}
			if (isBDPhone != null && isBDPhone == true) {
				if (!mTopUpMoney.getText().toString().trim().equals("")) {
					Intent intent = new Intent(getActivity(),TopUpActivity.class);
					intent.putExtra("money", mTopUpMoney.getText().toString().trim());
					startActivityForResult(intent, TOPUPCODE);
				}else {
					SingleBtnFragmentDialog.showDefault(getFragmentManager(), "请填写要充值的金额");
				}
			}else {
				doubleDialogFlag=2;
				DoubleBtnFragmentDialog.showDefault(getFragmentManager(), "使用钱包支付,需绑定手机并设置支付密码,您目前未绑定手机.", "稍后再说", "现在绑定", this);
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public void onDismiss(DialogFragment fragment) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(DialogFragment fragment, View v) {
		switch (doubleDialogFlag) {
		case 1://设置密码
			Intent intent = new Intent(getActivity(), PwdSettingActivity.class);
			intent.putExtra("isPayPwd", isPayPwd);
			intent.putExtra("isBDPhoneNum", isBDPhoneNum);
			startActivityForResult(intent, 1);
			break;
		case 2://绑定手机
			Intent intent1 = new  Intent(getActivity(), SettingPhoneBound.class);
			startActivityForResult(intent1, 2);
			break;
		default:
			break;
		}
		
	}
	
	@Override
	public void onActivityResult(int arg0, int arg1, Intent arg2) {
		if (arg0 == 2 && arg1 == getActivity().RESULT_OK) {
			isPayPwd = true;
		}else if(arg0 == 2 && arg1 == getActivity().RESULT_OK){
			if (arg2.hasExtra("phone_num")) {
				isBDPhone = true;
				isBDPhoneNum = arg2.getExtras().getString("phone_num");
			}else {
				isBDPhone = false;
			}
		}else if(arg0 == TOPUPCODE && arg1 == getActivity().RESULT_OK){
			initDate(v);
		}
		super.onActivityResult(arg0, arg1, arg2);
	}

	@Override
	public void onClickSureHander() {
		// TODO Auto-generated method stub
		if (isSettlement) {
			Intent intent = new Intent(getActivity(),SettlementActivity.class);
			intent.putExtra("settlementMessage", settlementMessage);
			intent.putExtra("money", money);
			intent.putExtra("isBDPhoneNum", isBDPhoneNum);
			if(!TextUtils.isEmpty(mBankNumber))
			intent.putExtra("bank", mBankNumber);
			startActivity(intent);
		}
	}
}
